package ${package.Controller};

import com.baomidou.mybatisplus.core.metadata.IPage;
import ${cfg.packageInfo}.common.HttpResult;
import ${cfg.packageInfo}.dto.ParamGroupDTO;
import ${cfg.packageInfo}.dto.input.${entity}InputDTO;
import ${cfg.packageInfo}.dto.input.Select${entity}InputDTO;
import ${cfg.packageInfo}.dto.output.${entity}OutputDTO;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
<#if cfg.swaggerFlag>
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
</#if>
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * ${table.comment!} 控制器
 * @author ${author}
 * @date ${date}
 */
@Validated
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
<#if cfg.swaggerFlag>
@Api("${table.comment!}")
</#if>
@RequestMapping("<#if cfg.modulePath??>${cfg.modulePath}</#if><#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Resource
    private ${entity}Service ${entity?uncap_first}Service;

    <#if cfg.swaggerFlag>
    @ApiOperation("添加${table.comment!}")
    <#else>
    /**
     * 添加${table.comment!}
     * @param inputDTO 输入DTO
     * @return
     */
    </#if>
    @PostMapping(value = "insert${entity}")
    public HttpResult insert${entity}(@Validated @RequestBody ${entity}InputDTO inputDTO) {
        ${entity} ${entity?uncap_first} = new ${entity}();
        BeanUtils.copyProperties(inputDTO, ${entity?uncap_first});
        boolean success = ${entity?uncap_first}Service.save(${entity?uncap_first});
        if (!success) {
            return HttpResult.fail("添加失败");
        }
        return HttpResult.success("添加成功");
    }

    <#if cfg.swaggerFlag>
    @ApiOperation("根据id修改${table.comment!}")
    <#else>
    /**
     * 根据id修改${table.comment!}
     * @param inputDTO 输入DTO
     * @return
     */
    </#if>
    @PostMapping(value = "update${entity}ById")
    public HttpResult update${entity}ById(@Validated(value = {ParamGroupDTO.Update.class})
                                            @RequestBody ${entity}InputDTO inputDTO) {
        ${entity} ${entity?uncap_first} = new ${entity}();
        BeanUtils.copyProperties(inputDTO, ${entity?uncap_first});
        boolean success = ${entity?uncap_first}Service.updateById(${entity?uncap_first});
        if (!success) {
            return HttpResult.fail("修改失败");
        }
        return HttpResult.success("修改成功");
    }

    <#if cfg.swaggerFlag>
    @ApiOperation("根据id删除${table.comment!}")
    <#else>
    /**
     * 根据id删除${table.comment!}
     * @param id 主键id
     * @return
     */
    </#if>
    @GetMapping(value = "delete${entity}ById")
    public HttpResult delete${entity}ById(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id) {
        boolean success = ${entity?uncap_first}Service.removeById(id);
        if (!success) {
            return HttpResult.fail("删除失败");
        }
        return HttpResult.success("删除成功");
    }

    <#if cfg.swaggerFlag>
    @ApiOperation("分页查询${table.comment!}列表")
    <#else>
    /**
     * 分页查询${table.comment!}列表
     * @param inputDTO 查询输入DTO
     * @return
     */
    </#if>
    @PostMapping(value = "select${entity}Page")
    public HttpResult select${entity}Page(@Validated @RequestBody Select${entity}InputDTO inputDTO) {
        ${entity}OutputDTO outputDTO = new ${entity}OutputDTO();
        IPage< ${entity} > page = ${entity?uncap_first}Service.select${entity}Page(inputDTO);

        outputDTO.set${entity}List(page.getRecords());
        outputDTO.setTotal(page.getTotal());
        return HttpResult.success("查询成功",outputDTO);
    }

    <#if cfg.swaggerFlag>
    @ApiOperation("分页查询${table.comment!}列表")
    <#else>
    /**
     * 普通查询${table.comment!}列表 无需分页
     * @param inputDTO 查询输入DTO
     * @return
     */
    </#if>
    @PostMapping(value = "select${entity}List")
    public HttpResult select${entity}List(@Validated @RequestBody Select${entity}InputDTO inputDTO) {
        ${entity}OutputDTO outputDTO = new ${entity}OutputDTO();
        List< ${entity} > ${entity?uncap_first}List = ${entity?uncap_first}Service.select${entity}List(inputDTO);

        outputDTO.set${entity}List(${entity?uncap_first}List);
        outputDTO.setTotal((long) ${entity?uncap_first}List.size());
        return HttpResult.success("查询成功",outputDTO);
    }


}